package com.student.service;

/**
 * Created by dongge on 16/9/17.
 */

import com.student.dao.*;
import com.student.entity.Menu;
import com.student.entity.Permission;
import com.student.entity.Role;
import com.student.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * 用来管理 用户 权限 角色 菜单的业务
 */
@Service("manageService")
@Transactional
public class ManageServiceImpl implements ManageService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RoleDao roleDao;

    public ManageServiceImpl() {
    }

    private CRUDSupport<?> getDaoByClass(Class cc){
        if(cc.equals(User.class)){
            return userDao;
        }else if(cc.equals(Menu.class)){
            return menuDao;
        }else if(cc.equals(Permission.class)){
            return permissionDao;
        }else if(cc.equals(Role.class)){
            return roleDao;
        }
        return null;
    }

    /*private CRUDSupport<?> getDaoByObject(Object o){
        if(o instanceof User){
            return userDao;
        }else if(o instanceof Menu){
            return menuDao;
        }else if(o instanceof Permission){
            return permissionDao;
        }else if(o instanceof Role){
            return roleDao;
        }
        return null;
    }*/

    public void save(Class cc, Object o){
        if(cc.equals(User.class)){
            userDao.save((User)o);
        }else if(cc.equals(Menu.class)){
            menuDao.save((Menu)o);
        }else if(cc.equals(Permission.class)){
            permissionDao.save((Permission)o);
        }else if(cc.equals(Role.class)){
            roleDao.save((Role)o);
        }
    }

    public void delete(Class cc, Serializable id){
        CRUDSupport<?> dao = getDaoByClass(cc);
        if(dao != null){
            dao.delete(id);
        }
    }

    public void update(Class cc, Object o){
        if(cc.equals(User.class)){
            userDao.update((User)o);
        }else if(cc.equals(Menu.class)){
            menuDao.update((Menu)o);
        }else if(cc.equals(Permission.class)){
            permissionDao.update((Permission)o);
        }else if(cc.equals(Role.class)){
            roleDao.update((Role)o);
        }
    }

    public Object get(Class cc, Serializable id){
        CRUDSupport<?> dao = getDaoByClass(cc);
        if(dao != null){
            return dao.get(id);
        }
        return  null;
    }

    public List<?> getAll(Class cc){
        CRUDSupport<?> dao = getDaoByClass(cc);
        if(dao != null){
            return dao.getAll();
        }
        return null;
    }

    public User getUserByUsername(String username){
        return userDao.getUserByUsername(username);
    }

    public Set<String> findRolesByUsername(String username){
        User user = getUserByUsername(username);
        HashSet<String> roleStrings = new HashSet<String>();
        for(Role role:user.getRoles()){
            roleStrings.add(role.getName());
        }
        return roleStrings;
    }

    public Set<String> findPermissionsByUsername(String username){
        User user = getUserByUsername(username);
        HashSet<String> permissionStrings = new HashSet<String>();
        for(Role role:user.getRoles()){
            for(Permission permission:role.getPermissons()){
                permissionStrings.add(permission.getName());
            }
        }
        return permissionStrings;
    }

}
